home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Tool Chest / Development Tools & Languages / Dylan Related / Marlais / Marlais 0.5.9-portable sources / boolean.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  3.7 KB  |  170 lines  |  [TEXT/ttxt]

  1. /*
  2.  
  3.    boolean.c
  4.  
  5.    This software is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public
  7.    License as published by the Free Software Foundation; either
  8.    version 2 of the License, or (at your option) any later version.
  9.  
  10.    This software is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    Library General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU Library General Public
  16.    License along with this software; if not, write to the Free
  17.    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.    Original copyright notice follows:
  20.  
  21.    Copyright, 1993, Brent Benson.  All Rights Reserved.
  22.    0.4 & 0.5 Revisions Copyright 1994, Joseph N. Wilson.  All Rights Reserved.
  23.  
  24.    Permission to use, copy, and modify this software and its
  25.    documentation is hereby granted only under the following terms and
  26.    conditions.  Both the above copyright notice and this permission
  27.    notice must appear in all copies of the software, derivative works
  28.    or modified version, and both notices must appear in supporting
  29.    documentation.  Users of this software agree to the terms and
  30.    conditions set forth in this notice.
  31.  
  32.  */
  33.  
  34. #include "boolean.h"
  35.  
  36. #include "prim.h"
  37.  
  38. /* primitives */
  39.  
  40. static Object not (Object obj);
  41.  
  42. static struct primitive boolean_prims[] =
  43. {
  44.     {"id?", prim_2_rest, id_p},
  45.     {"==", prim_2_rest, id_p},
  46.     {"not", prim_1, not},
  47.     {"~", prim_1, not},
  48. };
  49.  
  50. /* function definitions */
  51.  
  52. void
  53. init_boolean_prims (void)
  54. {
  55.     int num;
  56.  
  57.     num = sizeof (boolean_prims) / sizeof (struct primitive);
  58.  
  59.     init_prims (num, boolean_prims);
  60. }
  61.  
  62. #ifndef SMALL_OBJECTS
  63. Object
  64. make_true (void)
  65. {
  66.     Object obj;
  67.  
  68.     obj = allocate_object (sizeof (struct object));
  69.  
  70.     TYPE (obj) = True;
  71.     return (obj);
  72. }
  73. #else
  74. Object
  75. make_true (void)
  76. {
  77.     return (TRUEVAL);
  78. }
  79. #endif
  80.  
  81. #ifndef SMALL_OBJECTS
  82. Object
  83. make_false (void)
  84. {
  85.     Object obj;
  86.  
  87.     obj = allocate_object (sizeof (struct object));
  88.  
  89.     TYPE (obj) = False;
  90.     return (obj);
  91. }
  92. #else
  93. Object
  94. make_false (void)
  95. {
  96.     return (FALSEVAL);
  97. }
  98. #endif
  99.  
  100. Object
  101. id_p (Object obj1, Object obj2, Object rest)
  102. {
  103.     /* succeed quickly in the simple case */
  104.     if ((obj1 == obj2)) {
  105.     if (NULLP (rest)) {
  106.         return (true_object);
  107.     } else {
  108.         return id_p (obj2, CAR (rest), CDR (rest));
  109.     }
  110.     } else if (INTEGERP (obj1) && INTEGERP (obj2)) {
  111.     if (INTVAL (obj1) == INTVAL (obj2)) {
  112.         if (NULLP (rest)) {
  113.         return (true_object);
  114.         } else {
  115.         return (id_p (obj2, CAR (rest), CDR (rest)));
  116.         }
  117.     } else {
  118.         return (false_object);
  119.     }
  120.     } else if (CHARP (obj1) && CHARP (obj2)) {
  121.     if (CHARVAL (obj1) == CHARVAL (obj2)) {
  122.         if (NULLP (rest)) {
  123.         return (true_object);
  124.         } else {
  125.         return (id_p (obj2, CAR (rest), CDR (rest)));
  126.         }
  127.     } else {
  128.         return (false_object);
  129.     }
  130.     } else if (DFLOATP (obj1) && DFLOATP (obj2)) {
  131.     if (DFLOATVAL (obj1) == DFLOATVAL (obj2)) {
  132.         if (NULLP (rest)) {
  133.         return (true_object);
  134.         } else {
  135.         return (id_p (obj2, CAR (rest), CDR (rest)));
  136.         }
  137.     } else {
  138.         return (false_object);
  139.     }
  140.     } else {
  141.     return (false_object);
  142.     }
  143. }
  144.  
  145. int
  146. id (Object obj1, Object obj2)
  147. {
  148.     if (obj1 == obj2) {
  149.     return 1;
  150.     } else if (INTEGERP (obj1) && INTEGERP (obj2)) {
  151.     return (INTVAL (obj1) == INTVAL (obj2));
  152.     } else if (CHARP (obj1) && CHARP (obj2)) {
  153.     return (CHARVAL (obj1) == CHARVAL (obj2));
  154.     } else if (DFLOATP (obj1) && DFLOATP (obj2)) {
  155.     return (DFLOATVAL (obj1) == DFLOATVAL (obj2));
  156.     } else {
  157.     return 0;
  158.     }
  159. }
  160.  
  161. static Object
  162. not (Object obj)
  163. {
  164.     if (obj == false_object) {
  165.     return (true_object);
  166.     } else {
  167.     return (false_object);
  168.     }
  169. }
  170.